<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

$db = db_connect();

// 获取反馈信息
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$stmt = $db->prepare("SELECT f.*, u.name as user_name, u.phone as user_phone 
                      FROM feedbacks f 
                      LEFT JOIN users u ON f.user_id = u.id 
                      WHERE f.id = :id");
$stmt->bindValue(':id', $id);
$stmt->execute();
$feedback = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$feedback) {
    redirect('feedback.php', '反馈不存在', 'error');
}

// 反馈类型映射
$feedback_types = [
    'feature' => ['name' => '功能建议', 'badge' => 'badge-primary'],
    'bug' => ['name' => '问题反馈', 'badge' => 'badge-danger'],
    'other' => ['name' => '其他', 'badge' => 'badge-secondary']
];

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $reply = trim($_POST['reply']);
    $status = isset($_POST['status']) ? 1 : 2; // 如果勾选了状态，设为已回复，否则设为已关闭

    try {
        // 更新数据库
        $stmt = $db->prepare("UPDATE feedbacks SET reply = :reply, status = :status, 
                             reply_time = NOW(), admin_id = :admin_id, 
                             updated_at = NOW() WHERE id = :id");
        $stmt->bindValue(':reply', $reply);
        $stmt->bindValue(':status', $status);
        $stmt->bindValue(':admin_id', $_SESSION['id']);
        $stmt->bindValue(':id', $id);
        
        if ($stmt->execute()) {
            redirect('feedback.php', '回复成功');
        } else {
            redirect('feedback_reply.php?id=' . $id, '回复失败，请重试。', 'error');
        }
    } catch (Exception $e) {
        redirect('feedback_reply.php?id=' . $id, '系统错误：' . $e->getMessage(), 'error');
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回复反馈 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
    .card {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        padding: 25px;
        margin-bottom: 20px;
    }
    .feedback-info {
        margin-bottom: 20px;
        padding: 15px;
        background: #f8f9fa;
        border-radius: 4px;
    }
    .feedback-meta {
        display: flex;
        gap: 20px;
        color: #666;
        font-size: 0.9em;
        margin-bottom: 10px;
    }
    .feedback-content {
        margin: 15px 0;
        white-space: pre-wrap;
    }
    .feedback-images {
        display: flex;
        gap: 10px;
        margin-top: 15px;
    }
    .feedback-image {
        width: 100px;
        height: 100px;
        object-fit: cover;
        border-radius: 4px;
        cursor: pointer;
        transition: transform 0.2s;
    }
    .feedback-image:hover {
        transform: scale(1.05);
    }
    .form-group {
        margin-bottom: 20px;
    }
    .form-control {
        width: 100%;
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        transition: border-color 0.3s;
    }
    textarea.form-control {
        min-height: 150px;
        resize: vertical;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>回复反馈</h2>
                </div>
                <div class="header-right">
                    <a href="feedback.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-left"></i> 返回列表
                    </a>
                </div>
            </header>

            <div class="content">
                <!-- 反馈详情 -->
                <div class="card">
                    <div class="feedback-info">
                        <div class="feedback-meta">
                            <span>用户：<?php echo htmlspecialchars($feedback['user_name']); ?></span>
                            <span>电话：<?php echo htmlspecialchars($feedback['user_phone']); ?></span>
                            <span>时间：<?php echo date('Y-m-d H:i:s', strtotime($feedback['created_at'])); ?></span>
                            <span>类型：
                                <span class="badge <?php echo $feedback_types[$feedback['type']]['badge']; ?>">
                                    <?php echo $feedback_types[$feedback['type']]['name']; ?>
                                </span>
                            </span>
                        </div>
                        <h3><?php echo htmlspecialchars($feedback['title']); ?></h3>
                        <div class="feedback-content"><?php echo htmlspecialchars($feedback['content']); ?></div>
                        <?php if ($feedback['images']): ?>
                        <div class="feedback-images">
                            <?php foreach (explode(',', $feedback['images']) as $image): ?>
                            <img src="<?php echo '../api/user/' . htmlspecialchars($image); ?>" 
                                 class="feedback-image" 
                                 onclick="showImage(this.src)"
                                 alt="反馈图片">
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>
                    </div>

                    <?php if ($feedback['reply']): ?>
                    <div class="feedback-info">
                        <div class="feedback-meta">
                            <span>回复时间：<?php echo date('Y-m-d H:i:s', strtotime($feedback['reply_time'])); ?></span>
                        </div>
                        <div class="feedback-content"><?php echo htmlspecialchars($feedback['reply']); ?></div>
                    </div>
                    <?php endif; ?>

                    <!-- 回复表单 -->
                    <form action="" method="POST">
                        <div class="form-group">
                            <label for="reply">回复内容</label>
                            <textarea name="reply" id="reply" class="form-control" required><?php echo htmlspecialchars($feedback['reply']); ?></textarea>
                        </div>

                        <div class="form-group">
                            <label>
                                <input type="checkbox" name="status" value="1" 
                                       <?php echo $feedback['status'] == 1 ? 'checked' : ''; ?>>
                                标记为已回复（不勾选则关闭反馈）
                            </label>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-paper-plane"></i> 提交回复
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 图片预览模态框 -->
    <div id="imageModal" class="modal" onclick="this.style.display='none'">
        <img id="modalImage" class="modal-content">
    </div>

    <script>
    function showImage(src) {
        const modal = document.getElementById('imageModal');
        const modalImg = document.getElementById('modalImage');
        modal.style.display = "block";
        modalImg.src = src;
    }
    </script>

    <style>
    .modal {
        display: none;
        position: fixed;
        z-index: 1000;
        padding-top: 50px;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.9);
        cursor: pointer;
    }
    .modal-content {
        margin: auto;
        display: block;
        max-width: 80%;
        max-height: 80%;
    }
    </style>
</body>
</html> 